//
//  SectionNormalViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/8/6.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "SectionNormalViewController.h"
#import "AppealViewCell.h"
#import "SectionDetailController.h"
#import "SqDetailListViewController.h"
@interface SectionNormalViewController ()
<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
- (IBAction)leftBtn:(UIButton *)sender;
- (IBAction)rightBtn:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet BoardLabel *leftNumLabel;
@property (weak, nonatomic) IBOutlet BoardLabel *rightNumLabel;
@property (nonatomic, assign)int  page;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, copy) NSString *auth;
@end

@implementation SectionNormalViewController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"诉求列表";
    
    
    
    
    self.tableView.rowHeight = 100;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
     [self.tableView registerNib:[UINib nibWithNibName:@"AppealViewCell" bundle:nil] forCellReuseIdentifier:@"AppealViewCell11"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
           [self getListStatus:self.status];
       }];
       
       self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
           [self getListStatus:self.status];
       }];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.dataArray removeAllObjects];
    self.navigationController.navigationBar.hidden = NO;
    self.leftLabel.hidden = NO;
    self.rightLabel.hidden = YES;
    self.leftBtn.selected = YES;
    self.page =1;
    self.status = @"0";
    [self getListStatus:self.status];
       
}
#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AppealViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppealViewCell11" forIndexPath:indexPath];
    
      AppealModel *model = self.dataArray[indexPath.row];
       [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"tou"]];
       cell.bianLabel.text = [NSString stringWithFormat:@"编号:%@",model.appeal_code];
       cell.shenLabel.text = [NSString stringWithFormat:@"申请人:%@",model.real_name];
       cell.buLabel.text = [NSString stringWithFormat:@"部门:%@",model.section_name];
       cell.typeLabel.text = [NSString stringWithFormat:@"类型:%@",model.type_name];
       cell.timeLabel.text = [self ConvertStrToTime:model.create_time];
    if ([model.status isEqualToString:@"0"]) {
              cell.statusLabel.text = @"未处理";
              cell.statusLabel.backgroundColor = [UIColor colorWithHexString:@"007AFF"];
          }else if ([model.status isEqualToString:@"4"]) {
               cell.statusLabel.text = @"已完成";
              cell.statusLabel.backgroundColor = [UIColor colorWithHexString:@"007AFF"];
          }else if ([model.status isEqualToString:@"5"]||[model.status isEqualToString:@"6"]) {
               cell.statusLabel.text = @"驳回件";
              cell.statusLabel.backgroundColor = [UIColor redColor];
          }else if ([model.status isEqualToString:@"9"]) {
                  cell.statusLabel.text = @"已驳回";
              cell.statusLabel.backgroundColor = [UIColor colorWithHexString:@"007AFF"];
          }else{
              
               cell.statusLabel.text = @"待处理";
             cell.statusLabel.backgroundColor = [UIColor colorWithHexString:@"007AFF"];
          }
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AppealModel *model = self.dataArray[indexPath.row];
    
    if ([[TokenManager getLeader_main] isEqualToString:@"1"]) {
          if ([[TokenManager getIsheadOffice] isEqualToString:@"1"]) {
              self.auth = @"5";
          }else{
              self.auth = @"2";
          }
      }else if ([[TokenManager getSection_normal]isEqualToString:@"1"]){
          
          if ([[TokenManager getIsheadOffice]isEqualToString:@"1"]) {
              self.auth = @"6";
          }else{
              self.auth = @"3";
          }
      }
    
    
    
    [[ApiManager ShardInstance]admin_appeal_detailID:model.id staff_id:[TokenManager getUserId] auth:self.auth resultBlock:^(NSDictionary *data, NSError *error) {
        AllRsModel *model = [AllRsModel mj_objectWithKeyValues:data[@"rs"]];
        
        if ([self.status isEqualToString:@"0"]) {
            
            SectionDetailController *sqVC = [SectionDetailController loadStroyboard:[SectionDetailController new]];
            sqVC.model = model;
            [self.navigationController pushViewController:sqVC animated:YES];
            
        }else{
            
            SqDetailListViewController *sqVC = [SqDetailListViewController loadStroyboard:[SqDetailListViewController new]];
                       sqVC.model = model;
                [self.navigationController pushViewController:sqVC animated:YES];
            
        }
      
    }];
}

//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr{
    long long time=[timeStr longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
    
}


- (IBAction)leftBtn:(UIButton *)sender {
    [self.dataArray removeAllObjects];
    sender.selected = YES;
    self.leftLabel.hidden = NO;
    self.rightLabel.hidden = YES;
    self.rightBtn.selected = NO;
    self.page = 1;
    self.status = @"0";
    [self getListStatus:self.status];
}

- (IBAction)rightBtn:(UIButton *)sender {
    [self.dataArray removeAllObjects];
    sender.selected = YES;
    self.leftLabel.hidden = YES;
    self.rightLabel.hidden = NO;
    self.leftBtn.selected = NO;
    self.page = 1;
     self.status = @"1";
    [self getListStatus:self.status];
}

-(void) getListStatus:(NSString *)status{
    [[ToastManager shardInstance]showSystemProgress:self.view];
    
    WS(weakSelf);
    [[ApiManager ShardInstance]section_normal_appeal_liststaff_id:[TokenManager getUserId] page:self.page status:status resultBlock:^(NSDictionary *data, NSError *error) {
        
        [[ToastManager shardInstance]hideProgress:self.view];
        NSArray *array = [AppealModel mj_objectArrayWithKeyValuesArray:data[@"rs"][@"data"]];
        NumberModel *nuModel = [NumberModel mj_objectWithKeyValues:data[@"rs"][@"nums"]];
        
        if ([nuModel.status_0 isEqualToString:@"0"]) {
            weakSelf.leftNumLabel.hidden = YES;
        }else{
            
            weakSelf.leftNumLabel.hidden = NO;
            weakSelf.leftNumLabel.text = nuModel.status_0;
        }
        
        if ([nuModel.status_1 isEqualToString:@"0"]) {
            weakSelf.rightNumLabel.hidden = YES;
        }else{
            
            weakSelf.rightNumLabel.hidden = NO;
            weakSelf.rightNumLabel.text = nuModel.status_1;
        }
        if (array.count ==0) {
            weakSelf.tableView.mj_footer.alpha = 0.0;
        }else{
            weakSelf.tableView.mj_footer.alpha = 1.0;
        }
        
        [self.dataArray addObjectsFromArray:array];
        
        
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        weakSelf.page ++;
    }];
}
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
     return YES;
    
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed: self.dataArray?@"pg_score":@"pg_internet"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
   NSString *text = self.dataArray?@"暂无数据":@"出错啦~，点此重试";
       NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f],
                                    NSForegroundColorAttributeName: [UIColor blackColor]};
       return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

@end
